<?php

namespace Beerus\Modules\Peanut\Actions;

use Encore\Admin\Grid\Tools\BatchAction;

class TagManyStatus extends BatchAction
{
    protected $type;

    public function __construct($type = 0)
    {
        $this->type = $type;
    }

    public function script()
    {
        if ($this->type == 1 || $this->type == 2) {
            return <<<EOT
            function tagManySelectBatch()
            {
                var data = {
                    'uid': 0,
                    'back_ids': ''
                }
                var data = []
                $('.icheckbox_minimal-blue[aria-checked=true]').each(function(){
                    uid = $(this).parents('tr').find('#transfer-edit-info').attr('data-uid')
                    back_ids = $(this).parents('tr').find('#transfer-edit-info').attr('data-back_ids')
                    data.push({'uid':uid, 'back_ids': back_ids})
                })
                return data;
            }
            
            $('{$this->getElementClass()}').on('click', function() {
                           console.log(tagManySelectBatch())
                           swal({
                                title: "确定要操作已选择的数据吗？",
                                type: "warning",
                                showCancelButton: true,
                                confirmButtonColor: "#DD6B55",
                                confirmButtonText: "确认",
                                showLoaderOnConfirm: true,
                                cancelButtonText: "取消",
                                preConfirm: function() {
                                console.log($.admin.grid);
                                //ajax
                                     $.ajax({
                                        method: 'post',
                                        url: '/admin/Peanut/Transfer/OperateBatch',
                                        data: {
                                            _token:'{$this->getToken()}',
                                            back_data: tagManySelectBatch(),
                                            type: {$this->type}
                                        },
                                        success: function (e) {
                                            if (e.code == 1) {
                                                swal({
                                                    title: e.title,
                                                    type: "success",
                                                });
                                                setTimeout(function () {
                                                    $.pjax.reload('#pjax-container');
                                                }, 1000);
                                            } else {
                                                toastr.error(e.title, null, {
                                                    timeOut : 3000
                                                }); 
                                            }
                                        }
                                    });
                              //ajax-end
                             }
            
                            });
                });
                
        EOT;
        } elseif ($this->type == 3) {
            return <<<EOT
            function tagManySelectBatch()
            {
                var data = {
                    'uid': 0,
                    'back_ids': ''
                }
                var data = []
                $('.icheckbox_minimal-blue[aria-checked=true]').each(function(){
                    uid = $(this).parents('tr').find('#transfer-edit-info').attr('data-uid')
                    back_ids = $(this).parents('tr').find('#transfer-edit-info').attr('data-back_ids')
                    data.push({'uid':uid, 'back_ids': back_ids})
                })
                return data;
            }
            
            var inputOptions = new Promise(function(resolve) {
              resolve({
                  '云账户信息有误': '云账户信息有误'
                });
            });
            $('{$this->getElementClass()}').on('click', function() {
                           console.log(tagManySelectBatch())
                           swal({
                              title: '转账失败原因',
                              input: 'radio',
                              showCancelButton: true,
                              cancelButtonText: '取消',
                              confirmButtonText: '提交',
                              showLoaderOnConfirm: true,
                              inputOptions: inputOptions,
                              inputValue: "云账户信息有误",
                              inputValidator: function(result) {
                                if (result) {
                                     //ajax
                                     $.ajax({
                                        method: 'post',
                                        url: '/admin/Peanut/Transfer/OperateBatch',
                                        data: {
                                            _token:'{$this->getToken()}',
                                            back_data: tagManySelectBatch(),
                                            type: {$this->type},
                                            remark: result
                                        },
                                        success: function (e) {
                                            if (e.code == 1) {
                                                swal({
                                                    title: e.title,
                                                    type: "success",
                                                });
                                                setTimeout(function () {
                                                    $.pjax.reload('#pjax-container');
                                                }, 1000);
                                            } else {
                                                toastr.error(e.title, null, {
                                                    timeOut : 3000
                                                }); 
                                            }
                                        }
                                    });
                                    //ajax-end
                                } else {
                                    return new Promise(function(reject) {
                                        reject('请选择一个失败原因!');
                                   });
                                }
                             }
                            });
                });
                
        EOT;
        }

    }
}